package game.module.hero.dao;

import game.entity.Hero;
import game.module.hero.bean.Role;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class HeroDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public Hero getHeroById(int id) {
		return jdbcTemplate.queryForObject("select * from t_character where f_id = ?", new Object[] { id }, new RowMapper<Hero>() {

			@Override
			public Hero mapRow(ResultSet rs, int rowNum) throws SQLException {
				Hero hero = new Hero();
				hero.setId(rs.getInt("f_id"));
				hero.setAccountId(rs.getInt("f_account_id"));
				hero.setAttackAddpoint(rs.getInt("f_attack_addpoint"));
				hero.setCareerId(rs.getInt("f_popsinger"));
				hero.setCopper(rs.getInt("f_copper"));
				hero.setDefenceAddpoint(rs.getInt("f_defence_addpoint"));
				hero.setGrade(rs.getShort("f_grade"));
				hero.setHeadimg(rs.getByte("f_headimg"));
				hero.setName(rs.getString("f_name"));
				hero.setNextExperience(rs.getLong("f_next_experience"));
				hero.setNowExperience(rs.getLong("f_now_experience"));
				hero.setSex(rs.getByte("f_sex"));
				hero.setStrongAddpoint(rs.getInt("f_strong_addpoint"));
				return hero;
			}
		});
	}

	public void updateHero(Hero hero) {

	}

	public void batchUpdateHero(final Queue<Hero> heroList) {
		jdbcTemplate
				.batchUpdate(
						"update t_character set f_attack_addpoint=?,f_popsinger=?,f_copper=?,f_defence_addpoint=?,f_grade=?,f_headimg=?,f_name=?,f_next_experience=?,f_now_experience=?,f_sex=?,f_strong_addpoint=? where f_id = ?",
						new BatchPreparedStatementSetter() {

							@Override
							public void setValues(PreparedStatement ps, int i) throws SQLException {
								Hero hero = heroList.poll();
								if (hero != null) {
									ps.setInt(1, hero.getAttackAddpoint());
									ps.setInt(2, hero.getCareerId());
									ps.setInt(3, hero.getCopper());
									ps.setInt(4, hero.getDefenceAddpoint());
									ps.setShort(5, hero.getGrade());
									ps.setByte(6, hero.getHeadimg());
									ps.setString(7, hero.getName());
									ps.setLong(8, hero.getNextExperience());
									ps.setLong(9, hero.getNowExperience());
									ps.setByte(10, hero.getSex());
									ps.setInt(11, hero.getStrongAddpoint());
									ps.setInt(12, hero.getId());
								}
							}

							@Override
							public int getBatchSize() {
								return heroList.size();
							}
						});
	}

	public List<Integer> getRoleIdList() {
		return jdbcTemplate.query("select f_id from t_character", new ResultSetExtractor<List<Integer>>() {
			@Override
			public List<Integer> extractData(ResultSet rs) throws SQLException, DataAccessException {
				List<Integer> heroIdList = new ArrayList<Integer>();
				while (rs.next()) {
					int heroId = rs.getInt("f_id");
					heroIdList.add(heroId);
				}
				return heroIdList;
			}
		});
	}

	public List<Role> getRolesByUserId(Long id) {
		return null;
	}

	public Role insert(Role role) {
		return null;
	}

	public Hero getRoleByName(String roleName) {
		return null;
	}

}
